package cn.hospital.seviceimpl;

import cn.hospital.dao.MeaningUserMapper;
import cn.hospital.entity.MeaningUser;
import cn.hospital.service.LoginRegisteredService;
import cn.hospital.util.MD5;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginRegisteredServiceImpl implements LoginRegisteredService {
    @Resource
    private MeaningUserMapper meaningUserMapper;

    /**
     * 查重手机号
     *
     * @param userPhone 手机号
     * @return
     */
    public Integer findByPhone(String userPhone) {
        return meaningUserMapper.findByPhone(userPhone);
    }


    /**
     * 查重邮箱
     * @param userEmail 邮箱
     * @return
     */
    public Integer findByEmail(String userEmail) {
        return meaningUserMapper.findByEmail(userEmail);
    }


    /**
     * 注册方法
     * @param meaningUser 医生实体类
     * @return
     */
    public Integer addByMeaningUser(MeaningUser meaningUser
    ) {
        meaningUser.setUserDatetime(new Date());
        //对密码进行加密操作
        meaningUser.setPassword(MD5.getMd5(meaningUser.getPassword(), 32));
        meaningUser.setUserStatus(1);
        meaningUser.setUserState(2);
        return meaningUserMapper.addByMeaningUser(meaningUser);
    }

    /**
     * 登录方法
     * @param name     电话,邮箱,用户名
     * @param password 密码
     * @return
     */
    public Map<String, Object> findByMeaningUser(String name, String password, HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        String test = "\\^[0-9]*$\\";
        Integer count = 0;
        MeaningUser meaningUser = new MeaningUser();
        if ("admin".equals(name)) { //是管理员
            meaningUser = meaningUserMapper.findByMeaningUser("", "", name);
        } else if (name.indexOf("@") == -1 || test.matches(name)) { //不是邮箱，是手机号
            meaningUser = meaningUserMapper.findByMeaningUser(name, "", "");
        } else if (name.indexOf("@") != -1 || !test.matches(name)) {    //不是手机号，是邮箱
            meaningUser = meaningUserMapper.findByMeaningUser("", name, "");
        }
        /*判断用户名输入的密码是否与数据库中密码匹配*/
        if (meaningUser == null) {
            count = 2;  //用户名不存在
        } else {
            if (meaningUser.getPassword().equals(MD5.getMd5(password, 32))) {
                if (meaningUser.getUserStatus() == 1) {
                    count = 3;  //用户审核中
                } else if (meaningUser.getUserStatus() == 3) {
                    count = 4;  //用户审核未通过
                } else {
                    session.setAttribute("meaningUser", meaningUser);
                    session.setMaxInactiveInterval(60 * 60 * 60);
                    count = 1;  //用户登录成功
                }
            } else {
                count = 5;
            }
        }
        map.put("meaningUser", meaningUser);
        map.put("count", count);
        return map;
    }

    /**
     * 修改密码，验证旧密码
     * @param password
     * @param id
     * @return
     */
    @Override
    public int eq(String password, Integer id) {
        Integer row=0;
        MeaningUser meaningUser=meaningUserMapper.selectuserbyid(id);
        System.out.println(meaningUser);
        String password1=meaningUser.getPassword();
        System.out.println(password1);
        String pass= MD5.getMd5(password,32);
        if(pass.equals(password1)){
            return row=1;

        }
        return row=2;
    }

}
